{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "默认的权重初始化是以0.1为标准差的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "接下来采用Xavier初始化，即使得标准差为1/n，由于每一次训练是100条数据，因此调整参数为0.01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-1-6a46388b1d4b>:14: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From <ipython-input-1-6a46388b1d4b>:79: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See @{tf.nn.softmax_cross_entropy_with_logits_v2}.\n",
      "\n",
      "step 100, entropy loss: 0.263835, l2_loss: 424.055542, total loss: 0.293519\n",
      "0.93\n",
      "step 200, entropy loss: 0.261358, l2_loss: 427.185364, total loss: 0.291261\n",
      "0.94\n",
      "step 300, entropy loss: 0.110563, l2_loss: 429.150085, total loss: 0.140604\n",
      "0.97\n",
      "step 400, entropy loss: 0.126751, l2_loss: 430.656525, total loss: 0.156897\n",
      "0.98\n",
      "step 500, entropy loss: 0.039844, l2_loss: 431.734711, total loss: 0.070065\n",
      "1.0\n",
      "step 600, entropy loss: 0.032301, l2_loss: 432.872742, total loss: 0.062602\n",
      "0.99\n",
      "step 700, entropy loss: 0.050668, l2_loss: 433.825897, total loss: 0.081036\n",
      "1.0\n",
      "step 800, entropy loss: 0.092751, l2_loss: 434.484589, total loss: 0.123165\n",
      "0.98\n",
      "step 900, entropy loss: 0.034379, l2_loss: 435.108917, total loss: 0.064837\n",
      "0.99\n",
      "step 1000, entropy loss: 0.039074, l2_loss: 435.654236, total loss: 0.069570\n",
      "1.0\n",
      "0.9796\n",
      "step 1100, entropy loss: 0.024200, l2_loss: 436.115356, total loss: 0.054728\n",
      "0.99\n",
      "step 1200, entropy loss: 0.121680, l2_loss: 436.652069, total loss: 0.152245\n",
      "0.99\n",
      "step 1300, entropy loss: 0.027101, l2_loss: 437.086212, total loss: 0.057697\n",
      "1.0\n",
      "step 1400, entropy loss: 0.031342, l2_loss: 437.366425, total loss: 0.061958\n",
      "0.99\n",
      "step 1500, entropy loss: 0.028142, l2_loss: 437.767212, total loss: 0.058785\n",
      "1.0\n",
      "step 1600, entropy loss: 0.016585, l2_loss: 438.031738, total loss: 0.047247\n",
      "1.0\n",
      "step 1700, entropy loss: 0.071398, l2_loss: 438.349640, total loss: 0.102082\n",
      "1.0\n",
      "step 1800, entropy loss: 0.058227, l2_loss: 438.676270, total loss: 0.088934\n",
      "1.0\n",
      "step 1900, entropy loss: 0.029414, l2_loss: 438.893951, total loss: 0.060137\n",
      "1.0\n",
      "step 2000, entropy loss: 0.031919, l2_loss: 439.056305, total loss: 0.062653\n",
      "1.0\n",
      "0.9865\n",
      "step 2100, entropy loss: 0.091664, l2_loss: 439.140564, total loss: 0.122403\n",
      "0.98\n",
      "step 2200, entropy loss: 0.050529, l2_loss: 439.274170, total loss: 0.081278\n",
      "1.0\n",
      "step 2300, entropy loss: 0.006594, l2_loss: 439.612610, total loss: 0.037367\n",
      "0.99\n",
      "step 2400, entropy loss: 0.034237, l2_loss: 439.718018, total loss: 0.065018\n",
      "1.0\n",
      "step 2500, entropy loss: 0.022726, l2_loss: 439.830902, total loss: 0.053514\n",
      "1.0\n",
      "step 2600, entropy loss: 0.070001, l2_loss: 439.982452, total loss: 0.100800\n",
      "0.98\n",
      "step 2700, entropy loss: 0.037102, l2_loss: 439.984497, total loss: 0.067901\n",
      "0.99\n",
      "step 2800, entropy loss: 0.018658, l2_loss: 440.074432, total loss: 0.049464\n",
      "0.99\n",
      "step 2900, entropy loss: 0.061142, l2_loss: 440.177734, total loss: 0.091954\n",
      "0.99\n",
      "step 3000, entropy loss: 0.014805, l2_loss: 440.208618, total loss: 0.045619\n",
      "1.0\n",
      "0.987\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import argparse\n",
    "import sys\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "\n",
    "data_dir = '/tmp/tensorflow/mnist/input_data'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "y_ = tf.placeholder(tf.float32, [None, 10])\n",
    "learning_rate = tf.placeholder(tf.float32)\n",
    "\n",
    "#构造一个reshape函数，重构输入图片数据大小\n",
    "with tf.name_scope('reshape'):\n",
    "    x_image = tf.reshape(x, [-1, 28, 28, 1])\n",
    "\n",
    "#第一个卷积层\n",
    "with tf.name_scope('conv1'):\n",
    "    shape = [6, 6, 1, 32]  \n",
    "    W_conv1 = tf.Variable(tf.truncated_normal(shape, stddev=0.1),\n",
    "                        collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    shape = [32]\n",
    "    b_conv1 = tf.Variable(tf.constant(0.1, shape=shape))\n",
    "    l_conv1 = tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], \n",
    "                         padding='SAME') + b_conv1\n",
    "    h_conv1 = tf.nn.relu(l_conv1)\n",
    "\n",
    "with tf.name_scope('pool1'):\n",
    "    h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1],\n",
    "                        strides=[1, 2, 2, 1], padding='VALID')\n",
    "\n",
    "\n",
    "with tf.name_scope('conv2'):\n",
    "    W_conv2 = tf.Variable(tf.truncated_normal([6,6 , 32, 64], stddev=0.1),\n",
    "                        \n",
    "                        collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_conv2 = tf.Variable(tf.constant(0.1, shape=[64]))\n",
    "    l_conv2 = tf.nn.conv2d(h_pool1, W_conv2, strides=[1, 1, 1, 1], \n",
    "                         padding='SAME') + b_conv2\n",
    "    h_conv2 = tf.nn.relu(l_conv2)\n",
    "\n",
    "\n",
    "with tf.name_scope('pool2'):\n",
    "    h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1],\n",
    "                        strides=[1, 2, 2, 1], padding='VALID')\n",
    "\n",
    "\n",
    "with tf.name_scope('fc1'):\n",
    "    W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.01),\n",
    "                      \n",
    "                      collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]))\n",
    "\n",
    "    h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])\n",
    "    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)\n",
    "\n",
    "\n",
    "with tf.name_scope('dropout'):\n",
    "    keep_prob = tf.placeholder(tf.float32)\n",
    "    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)\n",
    "\n",
    "\n",
    "with tf.name_scope('fc2'):\n",
    "    W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.01),\n",
    "                      collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_fc2 = tf.Variable(tf.constant(0.1, shape=[10]))\n",
    "\n",
    "    y = tf.matmul(h_fc1_drop, W_fc2) + b_fc2\n",
    "\n",
    "\n",
    "cross_entropy = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))\n",
    "\n",
    "l2_loss = tf.add_n( [tf.nn.l2_loss(w) for w in tf.get_collection('WEIGHTS')] )\n",
    "total_loss = cross_entropy + 7e-5*l2_loss\n",
    "train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)\n",
    "\n",
    "sess = tf.Session()\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)\n",
    "\n",
    "\n",
    "for step in range(3000):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    lr = 0.1                                                                      #调整学习率为0.1\n",
    "    _, loss, l2_loss_value, total_loss_value = sess.run(\n",
    "               [train_step, cross_entropy, l2_loss, total_loss], \n",
    "               feed_dict={x: batch_xs, y_: batch_ys, learning_rate:lr, keep_prob:0.5})\n",
    "  \n",
    "    if (step+1) % 100 == 0:\n",
    "        print('step %d, entropy loss: %f, l2_loss: %f, total loss: %f' % \n",
    "            (step+1, loss, l2_loss_value, total_loss_value))\n",
    "    # Test trained model\n",
    "        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "        print(sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys, keep_prob:0.5}))\n",
    "    if (step+1) % 1000 == 0:\n",
    "        print(sess.run(accuracy, feed_dict={x: mnist.test.images,\n",
    "                                    y_: mnist.test.labels, keep_prob:0.5}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "正确率已经接近99%了！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用手动计算的MSRA初始化，即标准差为0.02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting /tmp/tensorflow/mnist/input_data\\train-images-idx3-ubyte.gz\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\t10k-labels-idx1-ubyte.gz\n",
      "step 100, entropy loss: 0.235367, l2_loss: 1208.989746, total loss: 0.319996\n",
      "0.97\n",
      "step 200, entropy loss: 0.128869, l2_loss: 1210.538696, total loss: 0.213606\n",
      "0.99\n",
      "step 300, entropy loss: 0.127855, l2_loss: 1211.179199, total loss: 0.212638\n",
      "0.99\n",
      "step 400, entropy loss: 0.238083, l2_loss: 1211.340210, total loss: 0.322877\n",
      "0.97\n",
      "step 500, entropy loss: 0.069828, l2_loss: 1211.308960, total loss: 0.154620\n",
      "1.0\n",
      "step 600, entropy loss: 0.103420, l2_loss: 1211.316406, total loss: 0.188213\n",
      "0.98\n",
      "step 700, entropy loss: 0.182405, l2_loss: 1210.897827, total loss: 0.267168\n",
      "0.98\n",
      "step 800, entropy loss: 0.097162, l2_loss: 1210.462036, total loss: 0.181895\n",
      "1.0\n",
      "step 900, entropy loss: 0.103458, l2_loss: 1209.895996, total loss: 0.188151\n",
      "0.98\n",
      "step 1000, entropy loss: 0.045589, l2_loss: 1209.310669, total loss: 0.130241\n",
      "0.98\n",
      "0.9833\n",
      "step 1100, entropy loss: 0.106167, l2_loss: 1208.688721, total loss: 0.190775\n",
      "0.99\n",
      "step 1200, entropy loss: 0.023228, l2_loss: 1208.054810, total loss: 0.107792\n",
      "1.0\n",
      "step 1300, entropy loss: 0.099169, l2_loss: 1207.401489, total loss: 0.183687\n",
      "0.99\n",
      "step 1400, entropy loss: 0.029958, l2_loss: 1206.701172, total loss: 0.114427\n",
      "1.0\n",
      "step 1500, entropy loss: 0.021892, l2_loss: 1205.957397, total loss: 0.106309\n",
      "1.0\n",
      "step 1600, entropy loss: 0.021529, l2_loss: 1205.047974, total loss: 0.105882\n",
      "1.0\n",
      "step 1700, entropy loss: 0.040843, l2_loss: 1204.287964, total loss: 0.125143\n",
      "1.0\n",
      "step 1800, entropy loss: 0.026711, l2_loss: 1203.426147, total loss: 0.110951\n",
      "0.99\n",
      "step 1900, entropy loss: 0.019590, l2_loss: 1202.626343, total loss: 0.103774\n",
      "1.0\n",
      "step 2000, entropy loss: 0.027872, l2_loss: 1201.665527, total loss: 0.111988\n",
      "1.0\n",
      "0.989\n",
      "step 2100, entropy loss: 0.112716, l2_loss: 1200.793091, total loss: 0.196771\n",
      "0.98\n",
      "step 2200, entropy loss: 0.026886, l2_loss: 1199.791992, total loss: 0.110872\n",
      "1.0\n",
      "step 2300, entropy loss: 0.025216, l2_loss: 1198.954712, total loss: 0.109143\n",
      "1.0\n",
      "step 2400, entropy loss: 0.086987, l2_loss: 1198.004517, total loss: 0.170847\n",
      "0.98\n",
      "step 2500, entropy loss: 0.005153, l2_loss: 1197.033325, total loss: 0.088945\n",
      "1.0\n",
      "step 2600, entropy loss: 0.005920, l2_loss: 1196.078613, total loss: 0.089646\n",
      "1.0\n",
      "step 2700, entropy loss: 0.021073, l2_loss: 1195.045288, total loss: 0.104726\n",
      "0.99\n",
      "step 2800, entropy loss: 0.009550, l2_loss: 1194.117798, total loss: 0.093139\n",
      "1.0\n",
      "step 2900, entropy loss: 0.026328, l2_loss: 1193.164307, total loss: 0.109850\n",
      "1.0\n",
      "step 3000, entropy loss: 0.061059, l2_loss: 1192.062500, total loss: 0.144503\n",
      "1.0\n",
      "0.988\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import argparse\n",
    "import sys\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "\n",
    "data_dir = '/tmp/tensorflow/mnist/input_data'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "y_ = tf.placeholder(tf.float32, [None, 10])\n",
    "learning_rate = tf.placeholder(tf.float32)\n",
    "\n",
    "#构造一个reshape函数，重构输入图片数据大小\n",
    "with tf.name_scope('reshape'):\n",
    "    x_image = tf.reshape(x, [-1, 28, 28, 1])\n",
    "\n",
    "#第一个卷积层\n",
    "with tf.name_scope('conv1'):\n",
    "    shape = [6, 6, 1, 32]  \n",
    "    W_conv1 = tf.Variable(tf.truncated_normal(shape, stddev=0.1),\n",
    "                        collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    shape = [32]\n",
    "    b_conv1 = tf.Variable(tf.constant(0.1, shape=shape))\n",
    "    l_conv1 = tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], \n",
    "                         padding='SAME') + b_conv1\n",
    "    h_conv1 = tf.nn.relu(l_conv1)\n",
    "\n",
    "with tf.name_scope('pool1'):\n",
    "    h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1],\n",
    "                        strides=[1, 2, 2, 1], padding='VALID')\n",
    "\n",
    "\n",
    "with tf.name_scope('conv2'):\n",
    "    W_conv2 = tf.Variable(tf.truncated_normal([6,6 , 32, 64], stddev=0.1),\n",
    "                        \n",
    "                        collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_conv2 = tf.Variable(tf.constant(0.1, shape=[64]))\n",
    "    l_conv2 = tf.nn.conv2d(h_pool1, W_conv2, strides=[1, 1, 1, 1], \n",
    "                         padding='SAME') + b_conv2\n",
    "    h_conv2 = tf.nn.relu(l_conv2)\n",
    "\n",
    "\n",
    "with tf.name_scope('pool2'):\n",
    "    h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1],\n",
    "                        strides=[1, 2, 2, 1], padding='VALID')\n",
    "\n",
    "\n",
    "with tf.name_scope('fc1'):\n",
    "    W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.02),\n",
    "                      \n",
    "                      collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]))\n",
    "\n",
    "    h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])\n",
    "    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)\n",
    "\n",
    "\n",
    "with tf.name_scope('dropout'):\n",
    "    keep_prob = tf.placeholder(tf.float32)\n",
    "    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)\n",
    "\n",
    "\n",
    "with tf.name_scope('fc2'):\n",
    "    W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.02),\n",
    "                      collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_fc2 = tf.Variable(tf.constant(0.1, shape=[10]))\n",
    "\n",
    "    y = tf.matmul(h_fc1_drop, W_fc2) + b_fc2\n",
    "\n",
    "\n",
    "cross_entropy = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))\n",
    "\n",
    "l2_loss = tf.add_n( [tf.nn.l2_loss(w) for w in tf.get_collection('WEIGHTS')] )\n",
    "total_loss = cross_entropy + 7e-5*l2_loss\n",
    "train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)\n",
    "\n",
    "sess = tf.Session()\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)\n",
    "\n",
    "\n",
    "for step in range(3000):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    lr = 0.1                                                                      #调整学习率为0.1\n",
    "    _, loss, l2_loss_value, total_loss_value = sess.run(\n",
    "               [train_step, cross_entropy, l2_loss, total_loss], \n",
    "               feed_dict={x: batch_xs, y_: batch_ys, learning_rate:lr, keep_prob:0.5})\n",
    "  \n",
    "    if (step+1) % 100 == 0:\n",
    "        print('step %d, entropy loss: %f, l2_loss: %f, total loss: %f' % \n",
    "            (step+1, loss, l2_loss_value, total_loss_value))\n",
    "    # Test trained model\n",
    "        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "        print(sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys, keep_prob:0.5}))\n",
    "    if (step+1) % 1000 == 0:\n",
    "        print(sess.run(accuracy, feed_dict={x: mnist.test.images,\n",
    "                                    y_: mnist.test.labels, keep_prob:0.5}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "结果提升了一点，但总体已经完成了目标。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
